<?php

require_once 'vendor/autoload.php';

use Guzzle\Http\Client;

$client = new Client('http://9.131.184.140');

$requests = [];

//creating an array of all the links
$links = array(
//    "CapitalFM" => array(
//        "News" => "http://www.capitalfm.co.ke/business/feed/",
//        "Business" => "http://www.capitalfm.co.ke/news/feed/",
//        "Lifestyle" => "http://www.capitalfm.co.ke/lifestyle/feed/",
//        "Sports" => "http://www.capitalfm.co.ke/sports/feed",
//        "Ask Kirubi" => "http://www.capitalfm.co.ke/business/section/ask-kirubi/feed/",
//        "Technology" => "http://www.capitalfm.co.ke/business/section/technology/feed/",
//        "WorldBusiness" => "http://www.capitalfm.co.ke/business/section/world/feed/",
//        "Kenya Business" => "http://www.capitalfm.co.ke/business/section/kenya/feed/",
//        "Business-The White board" => "http://www.capitalfm.co.ke/business/section/whiteboard/feed/",
//        "Business-Finance" => "http://www.capitalfm.co.ke/business/section/finance/feed/",
//        "Business Enterprise" => "http://www.capitalfm.co.ke/business/section/enterprise/feed/",
//        "Jobs" => "http://jobsfinder.capitalfm.co.ke/pub/jobseeker/career/rss/rssfeed_latestjobs.xml",
//        "CapitalCampus" => "http://www.capitalfm.co.ke/campus/feed/"
//    ),
//    "Standard" => array(
//        "Headlines" => "http://www.standardmedia.co.ke/rss/headlines.php",
//        "Kenyan News" => "http://www.standardmedia.co.ke/rss/kenya.php",
//        "World News" => "http://www.standardmedia.co.ke/rss/world.php",
//        "Politics" => "http://www.standardmedia.co.ke/rss/politics.php",
//        "Sports" => "http://www.standardmedia.co.ke/rss/sports.php",
//        "Business" => "http://www.standardmedia.co.ke/rss/business.php",
//        "Columnists" => "http://www.standardmedia.co.ke/rss/columnists.php"
//    ),
    "Nation" => array(
        "News" => "http://www.nation.co.ke/news/-/1056/1056/-/view/asFeed/-/wr4x5yz/-/index.xml",
        "Africa News" => "http://www.nation.co.ke/news/africa/-/1066/1066/-/view/asFeed/-/vw1q0m/-/index.xml",
        "Politics" => "http://www.nation.co.ke/news/politics/-/1064/1064/-/view/asFeed/-/xeijmr/-/index.xml",
        "World News" => "http://www.nation.co.ke/news/world/-/1068/1068/-/view/asFeed/-/kira34z/-/index.xml",
        "Diaspora" => "http://www.nation.co.ke/news/diaspora/-/2107720/2107720/-/view/asFeed/-/11drhuu/-/index.xml",
        "Business" => "http://www.nation.co.ke/business/-/996/996/-/view/asFeed/-/14lpkvc/-/index.xml",
        "Counties" => "http://www.nation.co.ke/counties/-/1107872/1107872/-/view/asFeed/-/3ycob6/-/index.xml",
        "Sports" => "http://www.nation.co.ke/sports/-/1090/1090/-/view/asFeed/-/hlukmj/-/index.xml",
        "Blogs and Opinions" => "http://www.nation.co.ke/oped/-/1192/1192/-/view/asFeed/-/unsp8mz/-/index.xml",
        "Lifestyle" => "http://www.nation.co.ke/lifestyle/-/1190/1190/-/view/asFeed/-/tgsua2z/-/index.xml"
    )
);

//function to get the categories
function getCategory($value) {

    $categories = array();
    //categories arrays      
    $category_lists = new stdClass();
    $category_lists->{'News'} = array("News", "Headlines", "Kenyan News", "World News", "Politics", "Counties", "Africa News", "Politics", "World News", "Diaspora");
    $category_lists->{'Business'} = array("Business", "Ask Kirubi", "WorldBusiness", "Kenya Business", "Business-The White board", "Business-Finance", "Business Enterprise");
    $category_lists->{'Campus News'} =  array("CapitalCampus");
    $category_lists->{'Technology'} = array("Technology");
    $category_lists->{'Jobs'} = array("Jobs");
    $category_lists->{'Sports'} = array("Sports");
    $category_lists->{'Entertainment'} = array("Entertainment","Lifestyle");
    $category_lists->{'Blogs'} = array("Columnists", "Blogs and Opinions","Blogs","Opinions");
    
    echo PHP_EOL;
    
    foreach($category_lists as $category=>$list){
        if (in_array($value, $list)) {
            $categories[] = $category;
        }
    }
    
    if(empty($categories)){
        $categories[] = "General";
    }
    
    //return json_encode($categories);
    return array_shift($categories);
}

$p = 0;
foreach ($links as $each_member) {

    echo PHP_EOL;
    $p++;
    while (list($key, $value) = each($each_member)) {
        $xml = $value;
        //key eg News, Business
        echo $key . "===>";
        //xml is the link
        echo $xml . PHP_EOL;
        $xmlDoc = new DOMDocument();
        $xmlDoc->load($xml);
        $x = $xmlDoc->getElementsByTagName('item');


        for ($i = 0; $i < $x->length; $i++) {

            // $item_title=$x->item($i)->getElementsByTagName('title')->item($i)->childNodes;
            $item_title = $x->item($i)->getElementsByTagName('title')->item(0)->nodeValue;
            $item_link = $x->item($i)->getElementsByTagName('link')->item(0)->nodeValue;
            $item_desc = $x->item($i)->getElementsByTagName('description')->item(0)->nodeValue;

//            if ($p != 3) {
//                $item_date = $x->item($i)->getElementsByTagName('pubDate')->item(0)->nodeValue;
//            } else {
            $item_date = $x->item($i)->getElementsByTagName('date')->item(0)->nodeValue . PHP_EOL;
          //  }
            echo $item_link;
            echo $item_title;
            echo $item_desc;
            $date = new DateTime($item_date);
            echo $date->format('Y-m-d H:i:s');
            getCategory($key);
            echo "Successfully gotten the category " . getCategory($key);

            //putting the files into a txt file(filename, data to put);       

            $item = array(
                'link' => $item_link,
                'title' => $item_title,
                'description' => $item_desc,
                'published_at' => $date,
                //'category' => '',
                'category' => getCategory($key)
            );

            $item['published_at'] = $item['published_at']->format('Y-m-d H:i:s');
           
            
            $requests[] = $client->post('/api/v1/news/add', null, $item);
          


        }

    }
}
if (!empty($requests)) {
    $client->send($requests);
}



